<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Category as CategoryModel;

class Category extends Controller
{
    private $link;
    // 重载父类的初始化设置
    public function _initialize()
    {
        $this->link = new CategoryModel();
    }

    // 显示栏目列表
    public function index()
    {
        $parentId = input('get.parent_id', 0, 'intval');
        $res = $this->link->getFirstCategory($parentId);
        $this->assign('res',$res);
        return $this->fetch();
        // 为模版添加数据资源
        // return $this->fetch('',[
        //     'res' => $res,
        // ]);
    }

    // 添加栏目
    public function add()
    {
        $res = $this->link->getNormalFirstCategory();
        $this->assign('res',$res);
        return $this->fetch();
    }

    // 保存栏目
    public function save(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();// 获取所有的post参数
            $validate = $this->validate($data,'Category.add');// 实例化验证规则
            if(TRUE !== $validate){
                $this->error($validate);
            }
            if (!empty($data['id'])) {
                return $this->update($data);
            }
            $flag = $this->link->add($data);
            if ($flag) {
                $this->success('栏目添加成功！');
            }else{
                $this->error('Data Error！');
            }
        }else{
            $this->error('提交有误');
        }
    }

    // 编辑页面
    public function edit($id=0)
    {
        if (intval($id)<0) {
            $this->error('参数不合法！');
        }
        $category = $this->link->get($id);// 需要数据表主键为id
        $categorys = $this->link->getNormalFirstCategory();
        return $this->fetch('',[
            'category'=>$category,
            'categorys'=>$categorys,
        ]);
    }

    // 更新栏目
    public function update($data)
    {
        $res = $this->link->save($data,['id'=>intval($data['id'])]);
        if ($res) {
            $this->success('更新成功！');
        } else {
            $this->error('更新失败！');
        }

    }

    // 栏目排序
    public function listorder($id=0, $orderv=0)
    {
        $data = [
            'id'=>intval($id),
            'orderv'=>intval($orderv),
        ];
        $res = $this->link->save(['listorder'=>$data['orderv']],['id'=>$data['id']]);
        if ($res) {
            $this->result($_SERVER['HTTP_REFERER'], 1, '修改成功！');
        }
        $this->result($_SERVER['HTTP_REFERER'], 0, '修改失败！');
        // return json(['data'=>$data,'code'=>200,'message'=>'修改成功！']);
    }

    // 状态修改
    public function status(Request $request)
    {
        $data = $request->get();
        $validate = $this->validate($data,'Category.status');// 实例化验证规则
        if(TRUE !== $validate){
            $this->error($validate);
        }
        $res = $this->link->save(['status'=>$data['status']],['id'=>$data['id']]);
        if ($res) {
            $this->success('状态设置成功！');
        }
        $this->error('状态设置失败！');
    }
}
